#!/usr/bin/python
# -*- coding: cp1250 -*-

from pepe.glib import *

zf9 = lambda x: string.zfill(str(x), 9)
# 2683099 -> 002-683-099
x3x = lambda x: "%s-%s-%s" % (zf9(x)[:3], zf9(x)[3:6], zf9(x)[6:9])

n = '\x0D\x0A'

def main(filemask, minlen=1):
    print '''
    count delimiters'''
    minlen = int(minlen)

    for f in sorted(glob.glob(filemask)):
        errcnt = 0
        fbare = string.split(f, '\\')[-1]
        print '    ', timenow(), fbare

        fname = string.split(f, '\x5C')[-1]

        # 2010-10-22_2257 vp
        ## using underscore instead dot because .# utilities 
        ## cut out everything after dot and then it is not clear 
        ## whether utility was run on original file or on output
        ## of other .# utility that processed given file
        #of = open(fname + '.count_delimiters.repair#', 'wb')
        #ef = open('err_' + fname + '.count_delimiters.#', 'wb')
        
        #of = open(fname + '_count_delimiters_withoutbad', 'wb')
        #ef = open('err_' + fname + '_count_delimiters_badextracted.#', 'wb')
        #2011-06-14_1547 vip 
        of = open('cntd_new_' + fname, 'wb')
        ef = open('cntd_bad_' + fname, 'wb')

        cd = ''
        lino = 0
        cdcount = {}
        
        '''
        # handling file mask as ... f = aa-dataonly\*.dat
        fname = fw_slash(f)
        # fname = aa-dataonly/*.dat
        fname = string.split(fname, '/')[-1]
        # fname = *.dat
        fname = string.split(fname, '.')[0]
        # fname = *
        '''
        for line in bt.lines(f):
            lino += 1
            if cd == '': cd = findcoldelimiter(line)
            lenLine = len(string.split(line, cd))
            if lino == 1:
                lenHeaderLine = lenLine

            # search for errors
            if  (lenHeaderLine <> lenLine) or \
                len(re.sub('[ |\t]','',line)) <= int(minlen):
                
                errcnt += 1

                if (lenHeaderLine <> lenLine):
                    # line < lenHeaderLine
                    ef.write('Line number:\t%s' % (str(lino), ) + n)
                    ef.write('Columns in header count:\t%s' % (lenHeaderLine, ) + n)
                    ef.write('Columns in line count:\t%s' % (lenLine, ) + n)
                    ef.write("Line:\t%s" % (line) + n)
                    #sys.exit(2)
                else:
                    # probably line < minLine
                    ef.write('Line number:\t%s' % (str(lino), ) + n)
                    ef.write('Columns in header count:\t%s' % (lenHeaderLine, ) + n)
                    ef.write('Columns in line count:\t%s' % (lenLine, ) + n)
                    ef.write("Line:\t%s" % (line) + n)
                    #sys.exit(2)

            # no errors found
            else:
                pass
                of.write("%s" % (line))

        of.close()
        ef.close()
        #chid = iter.next()
        # report errors count
        if errcnt <> 0: print '\t', errcnt, ' errors in processing (%s)' % ('err_' + fname)
        print '    ', timenow()
    print n

if __name__ == "__main__":
    """"""
    if  len(sys.argv) == 2:
        main(filemask=sys.argv[1], minlen=1)
    elif len(sys.argv) == 3:
        main(filemask=sys.argv[1], minlen=sys.argv[2])
    else:
        print """

        count_delimiters.py <file-name> [<min-len>]

        file-name (mandatory)
            data filename to be processed

        min-length (optional)
            defines minimal number of non empty (non space or tab) characters 
            default 1 (row is reported as error if less than 1 non-empty /not space or tab/ chars)
        """

